package com.bc.mes.device.service.impl;

import com.bc.mes.common.api.CommonResult;
import com.bc.mes.common.dto.dv.DvMachineryTypeParam;
import com.bc.mes.device.service.DvMachineryTypeService;
import com.bc.mes.mbg.mapper.DvMachineryMapper;
import com.bc.mes.mbg.mapper.DvMachineryTypeMapper;
import com.bc.mes.mbg.model.dv.DvMachinery;
import com.bc.mes.mbg.model.dv.DvMachineryExample;
import com.bc.mes.mbg.model.dv.DvMachineryType;
import com.bc.mes.mbg.model.dv.DvMachineryTypeExample;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author zhangpingan
 * @date 2024/7/11
 * @desc
 */
@Service
public class DvMachineryTypeServiceImpl implements DvMachineryTypeService {

    @Autowired
    private DvMachineryTypeMapper dvMachineryTypeMapper;

    @Override
    public CommonResult<List<DvMachineryType>> list(String machineryTypeName, String enableFlag) {

        DvMachineryTypeExample dvMachineryTypeExample = new DvMachineryTypeExample();
        DvMachineryTypeExample.Criteria criteria = dvMachineryTypeExample.createCriteria();
        if (machineryTypeName != null && machineryTypeName != "") {
            criteria.andMachineryTypeNameLike("%" + machineryTypeName + "%");
        }

        if (enableFlag != null && enableFlag != "") {
            criteria.andEnableFlagEqualTo("%" + enableFlag + "%");
        }


        List<DvMachineryType> dvMachineryTypes = dvMachineryTypeMapper.selectByExample(dvMachineryTypeExample);

        return CommonResult.success(dvMachineryTypes);
    }

    @Override
    public CommonResult<DvMachineryType> selectById(Long id) {

        DvMachineryType dvMachineryType = dvMachineryTypeMapper.selectByPrimaryKey(id);
        return CommonResult.success(dvMachineryType);
    }

    @Override
    public int update(DvMachineryTypeParam dvMachineryTypeParam) {

        DvMachineryType dvMachineryType = new DvMachineryType();
        BeanUtils.copyProperties(dvMachineryTypeParam, dvMachineryType);
        return dvMachineryTypeMapper.updateByPrimaryKeySelective(dvMachineryType);
    }

    @Override
    public int insert(DvMachineryTypeParam dvMachineryTypeParam) {
        DvMachineryType dvMachineryType = new DvMachineryType();
        BeanUtils.copyProperties(dvMachineryTypeParam, dvMachineryType);
        return dvMachineryTypeMapper.insert(dvMachineryType);
    }

    @Override
    public CommonResult delete(Long id) {
        DvMachineryTypeExample dvMachineryTypeExample = new DvMachineryTypeExample();
        dvMachineryTypeExample.createCriteria().andParentTypeIdEqualTo(id);
        List<DvMachineryType> dvMachineryTypes = dvMachineryTypeMapper.selectByExample(dvMachineryTypeExample);
        if (dvMachineryTypes != null && !dvMachineryTypes.isEmpty()) {
            return CommonResult.failed("该设备类型下存在子类型，请先删除子类型");
        }
        int i = dvMachineryTypeMapper.deleteByPrimaryKey(id);
        return i > 0 ? CommonResult.success(i) : CommonResult.failed();

    }
}
